"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignHelper = void 0;
const Logger_1 = require("./Logger");
const Md5Helper_1 = require("./Md5Helper");
const HmacSha1 = require('crypto-js/hmac-sha1');
const Base64 = require('crypto-js/enc-base64');
const crypto = require("crypto");
class SignHelper {
    static toQueryString(params) {
        let keys = Object.keys(params);
        for (let i = keys.length - 1; i--; i >= 0) {
            if (keys[i] == "sign" || keys[i] == "" || !keys[i]) { //如果参数的值为空不参与签名；
                keys.splice(i, 1);
            }
        }
        keys.sort();
        let resultStr = "";
        for (let i = 0; i < keys.length; i++) {
            resultStr += keys[i] + "=" + params[keys[i]];
            if (i < keys.length - 1) {
                resultStr += "&";
            }
        }
        // Logger.log("resultStr==", resultStr)
        return resultStr;
    }
    // 安全签名
    static getXiaoMiSign(params, appSecret) {
        let md5AppSecret = Md5Helper_1.Md5Helper.getMd5Default(appSecret).toLowerCase();
        let signStr = this.toQueryString(params) + "&" + md5AppSecret;
        // Logger.log("getXiaoMiSign==", signStr)
        signStr = Md5Helper_1.Md5Helper.getMd5Default(signStr).toLowerCase();
        // Logger.log("getXiaoMiSign==2=", signStr)
        return signStr;
    }
    static getXiaoMiSign2(data, key) {
        let Authorization = Base64.stringify((HmacSha1(data, key)));
        Logger_1.Logger.log("getXiaoMiSign2=", Authorization);
        return Authorization;
    }
    // 安全签名
    static getVivoSign(params, appSecret) {
        let md5AppSecret = Md5Helper_1.Md5Helper.getMd5Default(appSecret).toLowerCase();
        let signStr = this.toQueryString(params) + "&" + md5AppSecret;
        // Logger.log("getSign==", signStr)
        signStr = Md5Helper_1.Md5Helper.getMd5Default(signStr).toLowerCase();
        // Logger.log("getSign==2=", signStr)
        return signStr;
    }
    /**
     * 签名验证（使用公钥、数据、签名）
     * @param content
     * @param sign
     * @param publicKey
     * @returns
     */
    static verifySign_rsa_sha1(content, sign, publicKey) {
        try {
            const verify = crypto.createVerify('RSA-SHA1');
            verify.update(content);
            verify.end();
            return verify.verify(publicKey, Buffer.from(sign, 'base64'));
        }
        catch (error) {
            Logger_1.Logger.log("verifySign error", error);
        }
    }
    static ysdk_encodeValue(value) {
        // 0~9 a~z A~Z !*() 处了这些之外 ，其他字符按其ASCII码的十六进制加%进行表示，例如“-”编码为“%2D”。
        let noChangeChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!*()";
        let result = "";
        for (let i = 0; i < value.length; i++) {
            let a = value.charAt(i);
            // Logger.log("encodeValue===", a)
            if (noChangeChar.includes(a)) {
                result += a;
            }
            else {
                let b = a.charCodeAt(0).toString(16).toUpperCase();
                result += "%" + b;
            }
        }
        // Logger.log("ysdk_encodeValue=", result)
        return result;
    }
    static ysdk_copyValue(params) {
        for (const key in params) {
            params[key] = this.ysdk_encodeValue(params[key]);
            // Logger.log("ysdk_copyValue", key, params[key])
        }
    }
    static toYsdkQueryString(params, deleteKey = "sign") {
        delete params[deleteKey];
        let keys = Object.keys(params);
        keys.sort();
        // Logger.log("toQueryString keys=222=",deleteKey, keys)
        let resultStr = "";
        for (let i = 0; i < keys.length; i++) {
            resultStr += keys[i] + "=" + params[keys[i]];
            if (i < keys.length - 1) {
                resultStr += "&";
            }
        }
        // Logger.log("resultStr==", resultStr)
        return resultStr;
    }
    static ysdk_makesig(method, url, params, secret) {
        let paramsStr = encodeURIComponent(this.toYsdkQueryString(params));
        paramsStr = paramsStr.replace(/\*/g, "%2A");
        paramsStr = paramsStr.replace(/\+/g, "%20");
        // let signStr: string = method.toUpperCase() + "&" + encodeURIComponent(url) + "&" + encodeURIComponent(this.toYsdkQueryString(params))
        let signStr = method.toUpperCase() + "&" + encodeURIComponent(url) + "&" + paramsStr;
        // Logger.log("ysdk_makesig==", signStr)
        return signStr;
    }
    // amt=10&appid=1112092318&appmeta=%7B%22shopId%22%3A1%2C%22productId%22%3A%22com.jhao.benniao.pay.1%22%2C%22orderId%22%3A%221634298414088-100018-7%22%2C%22attach%22%3A%22%22%2C%22amount%22%3A600%2C%22iosProductId%22%3A%22com.jhao.benniao.pay.1%22%2C%22prodoctName%22%3A%2260%C3%A9%C2%92%C2%BB%C3%A7%C2%9F%C2%B3%22%2C%22desc%22%3A%2260%C3%A9%C2%92%C2%BB%C3%A7%C2%9F%C2%B3%22%2C%22notifyUrl%22%3A%22https%3A%2F%2Fibirdgame.cn%3A9521%2Fysdk_pay%22%7D*wechat*wechat&billno=-APPDJSX108959-20211015-Jkx0AbHdYbXR&cftid=4200001198202110156933404041&channel_id=00000000-android-00000000-ysdk-ysdkwater-1-wechat&channel_orderid=4200001198202110156933404041&clientver=android&openid=oGtMy6hEYSV-_pgXilZcAIV87Owc&pay_channel=wechat&payamt_coins=0&paychannelsubid=1&payitem=com.jhao.benniao.pay.1*1*1&providetype=5&pubacct_payamt_coins=&tbazinga=1&token=F6E61A885CEF7A49161665D91C61DAD006168&ts=1634298429&uniacct_used_gen_amt=&version=v3&zoneid=1
    static verifyYsdkSign(method, url, params, secret, sig) {
        delete params["sig"];
        this.ysdk_copyValue(params);
        let signStr = this.ysdk_makesig(method, url, params, secret);
        let realSign = crypto.createHmac('sha1', secret).update(signStr).digest('base64'); //base64
        if (realSign == sig) {
            return true;
        }
        return false;
    }
}
exports.SignHelper = SignHelper;
